<template>
  <div
    class="base-table"
    :style="{ height: isCreate ? 'calc(100% - 102px)' : 'calc(100% - 72px)' }"
  >
    <div class="header">
      <slot name="header">
        <div class="title">
          <span>{{ title }}</span>
        </div>
        <div class="handler">
          <slot name="headerHandler"></slot>
        </div>
      </slot>
    </div>
    <el-table
      :data="listData"
      border
      style="width: 100%; height: 100%"
      height="100%"
      @selection-change="handleSelectionChange"
      v-bind="childrenProps"
    >
      <el-table-column
        v-if="showSelectColumn"
        align="center"
        type="selection"
        width="60"
      ></el-table-column>
      <el-table-column
        v-if="showIndexColumn"
        align="center"
        type="index"
        label="序号"
        width="80"
      />
      <template v-for="propItem in columnList" :key="propItem.prop">
        <el-table-column
          v-bind="propItem"
          :align="propItem.align || 'left'"
          show-overflow-tooltip
        >
          <template #default="scope">
            <slot :name="propItem.slotName" :row="scope.row">
              {{ scope.row[propItem.prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div v-if="showFooter" class="footer">
      <slot name="footer">
        <el-pagination
          :currentPage="pageInfo.currentPage"
          :page-size="pageInfo.pageSize"
          :page-sizes="pageInfo.pageOpt"
          v-bind="pagination"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import type { PropType } from "vue";

const props = defineProps({
  // 表格标题
  title: {
    type: String,
    default: ""
  },
  // 表格数据
  listData: {
    type: Array,
    required: true
  },
  // 数据总数
  listCount: {
    type: Number as PropType<number>,
    default: 0
  },
  // 表格列配置
  columnList: {
    type: Array as PropType<any[]>,
    required: true
  },
  // 是否展示索引列
  showIndexColumn: {
    type: Boolean,
    default: false
  },
  // 是否展示选择列
  showSelectColumn: {
    type: Boolean,
    default: false
  },
  // 分页配置
  pageInfo: {
    type: Object,
    default: () => ({ currentPage: 0, pageSize: 10 })
  },
  // 表格属性配置
  childrenProps: {
    type: Object,
    default: () => ({})
  },
  // 是否展示分页
  showFooter: {
    type: Boolean,
    default: true
  },
  // 分页器配置
  pagination: {
    type: Object,
    default: () => ({
      small: false,
      background: false,
      disabled: false
    })
  },
  // 是否显示新增按钮
  isCreate: {
    type: Boolean,
    default: false
  }
});

const emit = defineEmits([
  "selectionChange",
  "handleSizeChange",
  "handleCurrentChange"
]);

// 表格change事件
const handleSelectionChange = (value: any) => {
  emit("selectionChange", value);
};

// 分页条数改变
const handleSizeChange = (pageSize: number) => {
  emit("handleSizeChange", { ...props.pageInfo, pageSize });
};

// 页码改变
const handleCurrentChange = (currentPage: number) => {
  emit("handleCurrentChange", { ...props.pageInfo, currentPage });
};
</script>

<style lang="scss" scoped>
.header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
}
.title {
  font-size: 20px;
  font-weight: bold;
}
.footer {
  margin-top: 20px;
  text-align: center;
  display: flex;
  flex-direction: row-reverse;
}
</style>
